<?php

use console\db\Migration;

/**
 * 球迷学院用户相关数据表
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class m190102_180250_t_college_user extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 用户成就表
         * @table `college_user_achieve`
         */
        $this->tableBuilder([
            'name' => 'college_user_achieve',
            'comment' => '用户成就表',
            'columns' => [
                'id' => $this->primaryKey(),
                'achieve_id' => $this->integer(11)->unsigned()->notNull()->comment('成就ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->comment('用户ID'),
                'type' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(1)->comment('成就类型'),
                'date' => $this->timestamp()->notNull()->comment('获得日期'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(1)->comment('领取状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => ['achieve_id', 'user_id'],
                    'unique' => true,
                ]
            ]
        ]);

        /**
         * 用户关卡表
         * @table `college_user_chapter`
         */
        $this->tableBuilder([
            'name' => 'college_user_chapter',
            'comment' => '用户关卡表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->comment('用户ID'),
                'chapter_id' => $this->integer(11)->unsigned()->notNull()->comment('关卡ID'),
                'right_num' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('题目答对数量'),
                'star_num' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('星星数量'),
                'use_time' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('最优答题所用时间'),
                'first_time' => $this->dateTime()->notNull()->comment('首次完成时间'),
                'passed_at' => $this->unixTimestamp()->comment('首次通关时间'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(1)->comment('解锁状态：1解锁0未解锁'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => ['user_id', 'chapter_id'],
                    'unique' => true,
                ]
            ]
        ]);



        /**
         * 剩余答题次数表
         * @table `college_user_live`
         */
        $this->tableBuilder([
            'name' => 'college_user_live',
            'comment' => '剩余答题次数表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'share' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('分享次数最大2次'),
                'lives' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('剩余答题次数最大5次'),
                'date' => $this->date()->notNull()->comment('日期'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => ['user_id', 'date'],
                    'unique' => true,
                ]
            ]
        ]);

        /**
         * 用户答题情况表
         * @table `college_user_question`
         */
        $this->tableBuilder([
            'name' => 'college_user_question',
            'comment' => '用户答题情况表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'chapter_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('当前解锁关卡ID'),
                'achieve_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('当前勋章ID/最高等级'),
                'star_num' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('星星总数量'),
                'shar_num' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('分享总数量'),
                'use_time' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('游戏总的最优用时'),
                'answers_num' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('闯关总次数'),
                'first_time' => $this->dateTime()->notNull()->comment('首次玩游戏时间'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(1)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);



        /**
         * 用户答题表(日志表)
         * @table `college_user_answer`
         */
        $this->tableBuilder([
            'name' => 'college_user_answer',
            'comment' => '用户答题表(日志表)',
            'columns' => [
                'id' => $this->primaryKey(),
                'chapter_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('关卡ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'question_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('问题ID'),
                'answer' => $this->string(50)->notNull()->comment('选择答案'),
                'right' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('是否正确'),
                'time' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('用时'),
                'type' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('1取题2作答'),
                'date' => $this->unixTimestamp()->comment('答题日期'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 用户勋章表
         * @table `college_user_medal`
         */
        $this->tableBuilder([
            'name' => 'college_user_medal',
            'comment' => '用户勋章表',
            'columns' => [
                'id' => $this->primaryKey(),
                'medal_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('成就ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%college_user_medal}}');
        $this->dropTable('{{%college_user_answer}}');
        $this->dropTable('{{%college_user_question}}');
        $this->dropTable('{{%college_user_live}}');
        $this->dropTable('{{%college_user_chapter}}');
        $this->dropTable('{{%college_user_achieve}}');
    }

}
